#include "nodelet/nodelet.h"
#include "pluginlib/class_list_macros.h"
#include "ros/ros.h"
#include "std_msgs/Float64.h"

namespace nodelet_demo_ns
{
    class MyPlus : public nodelet::Nodelet
    {
    public:
        MyPlus()
        {
            value = 0.0;
        }
        void onInit()
        {
            //获取 NodeHandle
            ros::NodeHandle &nh = getPrivateNodeHandle();
            //从参数服务器获取参数
            nh.getParam("value", value);
            //创建发布与订阅对象
            pub = nh.advertise<std_msgs::Float64>("out", 100);
            sub = nh.subscribe<std_msgs::Float64>("in", 100, &MyPlus::doCb, this);
        }
        //回调函数
        void doCb(const std_msgs::Float64::ConstPtr &p)
        {
            // 1.解析数据
            double num = p->data;
            // 2.数据处理
            double result = num + value;
            // 3.发布
            std_msgs::Float64 r;
            r.data = result;
            pub.publish(r);
        }

    private:
        ros::Publisher pub;
        ros::Subscriber sub;
        double value;
    };
}
PLUGINLIB_EXPORT_CLASS(nodelet_demo_ns::MyPlus, nodelet::Nodelet)
